package com.example.mgsnew.Mgs;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import lombok.Data;

@Data
public class UploadProgress   {
    private Integer totalChunks;      // 总分片数
    private Integer uploadedChunks;   // 已上传分片数
    private Integer progress;         // 进度百分比(0-100)
    private String status;            // 状态(UPLOADING/COMPLETED/FAILED)
    private String fileUrl;           // 文件URL(上传完成后存在)
    private String errorMessage;      // 错误信息(失败时存在)

    // 新增时间戳字段
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Long createTime;      // 创建时间

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Long updateTime;      // 更新时间

    // 构造方法
    public UploadProgress() {
        this.createTime = System.currentTimeMillis();
        this.updateTime = createTime;
    }

    // 初始化时间戳
    @PrePersist
    public void prePersist() {
        this.createTime = System.currentTimeMillis();
        this.updateTime = this.createTime;
    }

    // 更新时间戳
    @PreUpdate
    public void preUpdate() {
        this.updateTime = System.currentTimeMillis();
    }

    // 失败状态构造
    public static UploadProgress fail(String taskId, String errorMessage) {
        UploadProgress progress = new UploadProgress();
        progress.setStatus("FAILED");
        progress.setErrorMessage(errorMessage);
        progress.setUpdateTime(System.currentTimeMillis());
        return progress;
    }

    // 完成状态构造
    public static UploadProgress complete(String taskId, String fileUrl, int totalChunks) {
        UploadProgress progress = new UploadProgress();
        progress.setStatus("COMPLETED");
        progress.setFileUrl(fileUrl);
        progress.setTotalChunks(totalChunks);
        progress.setUploadedChunks(totalChunks);
        progress.setProgress(100);
        progress.setUpdateTime(System.currentTimeMillis());
        return progress;
    }
}
